<ng-container *ngIf="!firstLoaded && loading">
  <i
    class="bwi bwi-spinner bwi-spin tw-text-muted"
    title="{{ 'loading' | i18n }}"
    aria-hidden="true"
  ></i>
  <span class="tw-sr-only">{{ "loading" | i18n }}</span>
</ng-container>
<ng-container *ngIf="sub">
  <bit-callout
    type="warning"
    title="{{ 'canceled' | i18n }}"
    *ngIf="subscription && subscription.cancelled"
  >
    {{ "subscriptionCanceled" | i18n }}</bit-callout
  >
  <bit-callout
    type="warning"
    title="{{ 'pendingCancellation' | i18n }}"
    *ngIf="subscriptionMarkedForCancel"
  >
    <p bitTypography="body1">{{ "subscriptionPendingCanceled" | i18n }}</p>
    <button
      bitButton
      type="button"
      buttonType="secondary"
      #reinstateBtn
      (click)="reinstate()"
      [appApiAction]="reinstatePromise"
      [disabled]="$any(reinstateBtn).loading()"
    >
      {{ "reinstateSubscription" | i18n }}
    </button>
  </bit-callout>
  <dl *ngIf="selfHosted">
    <dt>{{ "expiration" | i18n }}</dt>
    <dd *ngIf="sub.expiration">{{ sub.expiration | date: "mediumDate" }}</dd>
    <dd *ngIf="!sub.expiration">{{ "neverExpires" | i18n }}</dd>
  </dl>
  <div class="tw-flex tw-max-w-[1340px] tw-pt-6" *ngIf="!selfHosted">
    <div class="tw-flex tw-gap-16 tw-justify-between tw-w-full">
      <div class="tw-flex tw-flex-col">
        <div class="tw-font-semibold tw-mb-2">{{ "plan" | i18n }}</div>
        <div>{{ "premiumMembership" | i18n }}</div>
      </div>
      <div class="tw-flex tw-flex-col">
        <div class="tw-font-semibold tw-mb-2">{{ "status" | i18n }}</div>
        <div>
          <span class="tw-capitalize">{{ (subscription && subscriptionStatus) || "-" }}</span>
          <span
            bitBadge
            variant="warning"
            *ngIf="subscriptionMarkedForCancel"
            class="tw-mt-2 tw-block"
            >{{ "pendingCancellation" | i18n }}</span
          >
        </div>
      </div>
      <div class="tw-flex tw-flex-col">
        <div class="tw-font-semibold tw-mb-2 tw-text-right">{{ "nextChargeHeader" | i18n }}</div>
        <div>
          <ng-container *ngIf="subscription">
            <ng-container *ngIf="enableDiscountDisplay$ | async as enableDiscount; else noDiscount">
              <div class="tw-flex tw-items-center tw-gap-2 tw-flex-wrap tw-justify-end">
                <span [attr.aria-label]="'nextChargeDateAndAmount' | i18n">
                  {{
                    (sub.subscription.periodEndDate | date: "MMM d, y") +
                      ", " +
                      (discountedSubscriptionAmount | currency: "$")
                  }}
                </span>
                <billing-discount-badge
                  [discount]="getDiscountInfo(sub?.customerDiscount)"
                ></billing-discount-badge>
              </div>
            </ng-container>
            <ng-template #noDiscount>
              <div class="tw-flex tw-items-center tw-gap-2 tw-flex-wrap tw-justify-end">
                <span [attr.aria-label]="'nextChargeDateAndAmount' | i18n">
                  {{
                    (sub.subscription.periodEndDate | date: "MMM d, y") +
                      ", " +
                      (subscriptionAmount | currency: "$")
                  }}
                </span>
              </div>
            </ng-template>
          </ng-container>
          <span
            *ngIf="!subscription"
            class="tw-block tw-text-right"
            [attr.aria-label]="'noChargeScheduled' | i18n"
            >-</span
          >
        </div>
      </div>
    </div>
  </div>
  <ng-container *ngIf="selfHosted">
    <div>
      <button type="button" bitButton buttonType="secondary" (click)="updateLicense()">
        {{ "updateLicense" | i18n }}
      </button>
      <a
        bitButton
        buttonType="secondary"
        href="{{ this.cloudWebVaultUrl }}/#/settings/subscription"
        target="_blank"
        rel="noreferrer"
      >
        {{ "launchCloudSubscription" | i18n }}
      </a>
    </div>
  </ng-container>
  <div class="tw-max-w-[1340px]" *ngIf="!selfHosted">
    <h3 bitTypography="h3" class="tw-mt-8">{{ "storage" | i18n }}</h3>
    <p bitTypography="body1">
      {{ "subscriptionStorage" | i18n: sub.maxStorageGb || 0 : sub.storageName || "0 MB" }}
    </p>
    <bit-progress [barWidth]="storagePercentage" bgColor="success" size="default"></bit-progress>
    <ng-container *ngIf="subscription && !subscription.cancelled && !subscriptionMarkedForCancel">
      <div class="tw-mt-3">
        <div class="tw-flex tw-gap-4">
          <button bitButton type="button" buttonType="secondary" (click)="adjustStorage(true)">
            {{ "addStorage" | i18n }}
          </button>
          <button bitButton type="button" buttonType="secondary" (click)="adjustStorage(false)">
            {{ "removeStorage" | i18n }}
          </button>
        </div>
      </div>
    </ng-container>
    <h3 bitTypography="h3" class="tw-mt-16">{{ "additionalOptions" | i18n }}</h3>
    <p bitTypography="body1" class="tw-mt-3">{{ "additionalOptionsDesc" | i18n }}</p>
    <div class="tw-flex tw-gap-4 tw-mt-3">
      <button
        bitButton
        type="button"
        buttonType="secondary"
        (click)="downloadLicense()"
        *ngIf="!subscription || !subscription.cancelled"
      >
        {{ "downloadLicense" | i18n }}
      </button>
      <button
        bitButton
        #cancelBtn
        type="button"
        buttonType="danger"
        (click)="cancelSubscription()"
        [appApiAction]="cancelPromise"
        [disabled]="$any(cancelBtn).loading()"
        *ngIf="subscription && !subscription.cancelled && !subscriptionMarkedForCancel"
      >
        {{ "cancelSubscription" | i18n }}
      </button>
    </div>
  </div>
</ng-container>
